package com.weic.spark.scala.p2

import org.apache.spark.sql.SparkSession

/**
 * @Auther:BigData-weic
 * @ClassName:SparkSQLIntergedWithHive
 * @Date:2020/12/9 22:03
 * @功能描述: $FunctionDescription
 * @Version:1.0
 */
object SparkSQLIntergedWithHive {
	def main(args: Array[String]): Unit = {
		//模式匹配上传数据地址
		if (args == null || args.size != 2){
			println("aaa哈哈哈这个地址有问题，请输入争取的两个地址---basic_path---into_path")
			System.exit(-1)
		}
		val Array(basic_path,info_path) = args

		val spark = SparkSession.builder()
    		.appName("SparkSQLIntergedWithHive")
    		.master("local[*]")
    		.getOrCreate()
		//创建数据库
		spark.sql(
			"""
			  |create database if not exists test_0817
			  |""".stripMargin)
		//创建teacher_basic表
		spark.sql(
			"""
			  |create table if not exists `test_0817`.`teacher_basic`(
			  |name string,
			  |age int,
			  |married boolean,
			  |course int
			  |)row format delimited fields terminated by ","
			  |""".stripMargin)
		//创建teacher_info表
		spark.sql(
			"""
			  |create table if not exists `test_0817`.`teacher_info`(
			  |name string,
			  |height double
			  |)row format delimited fields terminated by ","
			  |""".stripMargin)
		//上传数据 basic
		spark.sql(
			s"""
			  |load data inpath '${basic_path}' into table `test_0817`.`teacher_basic`
			  |""".stripMargin)
		//上传数据 info
		spark.sql(
			s"""
			  |load data inpath '${info_path}' into table `test_0817`.`teacher_info`
			  |""".stripMargin)

		//查询数据
		val joined = spark.sql(
			"""
			  |select
			  |b.name,
			  |b.age,
			  |b.married,
			  |b.course,
			  |i.height
			  |from `test_0817`.`teacher_basic` b
			  |inner join `test_0817`.`teacher_info` i
			  |on b.name=i.name
			  |""".stripMargin)
		//数据写入指定表teacher
		joined.write.saveAsTable("`test_0817`.`teacher`")
		//释放资源
		spark.stop()
	}

}
